<?php
class Tender_LotController extends Agitum_Controller_Action
{
	public function indexAction() 
	{
	}
	
    public function activeAction() 
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout()->disableLayout();
		
		$user = Zend_Registry::get('Logged_User');
		
        $tour = $this->_getTour();
        $tender = $this->_getTender();
        $country = $this->_getCountry();
        
        $tender_id_list = array();
        $active = array();
        foreach ( $tender as $i => $row) {
        	$row['tour'] = array();
        	$row['country'] = array();
        	$tender_id_list[] = $row['id'];
        	if ( $row['owner_id'] == $user->id ) {
        		$row['is_owner'] = true;
        		unset($row['owner_id']);
        	}
        	$active[$row['id']] = $row;
        }
        
        if ( count($tender_id_list) ) 
        {
	        $_tour = Agitum_Model_Tender_Tour::listByTenderId($tender_id_list);
	        foreach($_tour as $i => $row) {
	        	$active[$row['src_id']]['tour'][$row['proposal']][] = $tour[$row['dst_id']]['title'];
	        }
	        
	        $_country = Agitum_Model_Tender_Country::listByTenderId($tender_id_list);
			foreach($_country as $i => $row) {
				$active[$row['src_id']]['country'][$row['proposal']][] = $country[$row['dst_id']]['title'];
	        }
        }

        $this->_response->setHeader('Content-Type', 'application/json');
		$this->_response->setBody(Zend_Json::encode(array_values($active)));
	}
	
	private function _getTender() 
	{
		$city_id = $this->_request->getParam('city', 0);
		$country_id = $this->_request->getParam('country', 0);
		$tour_id = $this->_request->getParam('tour', 0);
		
		$param = array('city' => $city_id, 'country' => $country_id, 'tour' => $tour_id);
        $slot = new Agitum_Model_Cache_Slot_Tender_List($param);
		$tender = $slot->load();
		if (!$tender) {
			$obj = new Agitum_Model_Tender();
			$tender = $obj->getActiveList($param);
			foreach( $tender as $i => $row ) {
				$slot->addTag(new Agitum_Model_Cache_Tag_Tender($row['id']));
			}
			$slot->addTag(new Agitum_Model_Cache_Tag_Tender_List($param));
		    $slot->save($tender);
		} else {
            $id_list = array();
            foreach ( $tender as $i => $row) {
            	$id_list[] = $row['id'];
            }
            $obj = new Agitum_Model_Tender();
			$data = $obj->getActiveTimes($id_list);
			$times = array();
			foreach ( $data as $i => $a) {
			    $times[$a['id']] = $a['timeto'];
			}
			for($i = 0; $i < count($tender); $i++) {
			    $tender[$i]['timeto'] = $times[$tender[$i]['id']];
			}
		}
		return $tender;
	}
	
	private function _getCountry() 
	{
		$slot = new Agitum_Model_Cache_Slot_Geo_List();
		$country = $slot->load();
		if (!$country) {
			$obj = new Agitum_Model_Geo();
			$country = $obj->fetchMapLevel($level = 1);
			foreach( $country as $i => $row ) {
				$slot->addTag(new Agitum_Model_Cache_Tag_Geo($row['id']));
			}
			$slot->addTag(new Agitum_Model_Cache_Tag_Geo_List());
			$slot->save($country);
		}
		return $country;
	}

	private function _getTour () 
	{
		// достаем список типов туров
		$slot = new Agitum_Model_Cache_Slot_Tour_List();
        $tour = $slot->load();
        if ( !$tour ) {
        	$obj = new Agitum_Model_Tour();
        	$tour= $obj->fetchMap();
        	foreach ( $tour as $id => $row ) {
        		$slot->addTag(new Agitum_Model_Cache_Tag_Tour($id));
        	}
        	$slot->addTag(new Agitum_Model_Cache_Tag_Tour_List());
        	$slot->save($tour);
        }
        return $tour;
	}
}